package com.example.springbootdemo2023.bus.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootdemo2023.bus.organization.dto.Organization;
import com.example.springbootdemo2023.bus.organization.mapper.OrganizationMapper;
import com.example.springbootdemo2023.bus.organization.service.IOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootdemo2023.core.dto.MyPageBand;
import com.example.springbootdemo2023.core.dto.MyResult;
import com.example.springbootdemo2023.core.util.MySysUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-04-07
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

    @Override
    public MyResult pages(Organization organization, MyPageBand page) {
        MyResult result = new MyResult();

        OrganizationMapper organizationMapper = getBaseMapper();

        List<Organization> list = organizationMapper.list(organization);

        result.setMsg("查询成功！");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult add(Organization organization) {

        MyResult result = new MyResult();

        OrganizationMapper organizationMapper = getBaseMapper();

        //编号重复查询
        QueryWrapper<Organization> queryWrap = new QueryWrapper<>();
        queryWrap.eq("code",organization.getCode());

        List<Organization> stations = organizationMapper.selectList(queryWrap);

        if(stations.size()>0){
            result.setMsg("编号["+organization.getCode()+"]重复，请重新换一个编号！");
            result.setCode("9999");
            return result;
        }

        //创建内部编码
        String innerCode = MySysUtil.getInnerCode("t_organization", "organizationID",organization.getParentID());
        organization.setInnerCode(innerCode);

        int insert = organizationMapper.insert(organization);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(Organization organization) {

        MyResult result = new MyResult();

        OrganizationMapper organizationMapper = getBaseMapper();

        //先查询出旧的组织信息
        Organization oldOrg = organizationMapper.selectById(organization.getOrganizationID());
        //如果父级没有发生变化内部编码就不改变
        if(!(oldOrg.getParentID().equals(organization.getParentID()))){

            //生成新的内部编码
            String innerCode = MySysUtil.getInnerCode("t_organization", "organizationID",organization.getParentID());
            organization.setInnerCode(innerCode);

            int update = organizationMapper.updateById(organization);

            //修改子集数据
            int i = organizationMapper.updateSonsInnerCode(oldOrg.getInnerCode(), innerCode);

            if(update>0){
                result.setMsg("修改成功！");
            }else{
                result.setMsg("修改失败！");
                result.setCode("9999");
            }
        }else{
            int update = organizationMapper.updateById(organization);

            if(update>0){
                result.setMsg("修改成功！");
            }else{
                result.setMsg("修改失败！");
                result.setCode("9999");
            }
        }

        return result;
    }

    @Override
    public MyResult delete(String idss) {
        //stationIDs 是内部编码集合
        MyResult result = new MyResult();

        List<String> ids = Arrays.asList(idss.split(","));

        OrganizationMapper organizationMapper = getBaseMapper();

        AtomicBoolean isDelets = new AtomicBoolean(false);

        ids.forEach(innerCode->{
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            queryWrapper.likeRight("innerCode",innerCode);
            int delete = organizationMapper.delete(queryWrapper);
            if(delete>0){
                isDelets.set(true);
            }else{
                isDelets.set(false);
            }
        });

        if(isDelets.get()){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
